package com.panda.business.service;

import com.panda.business.domain.AccountInfo;
import com.panda.business.domain.DeviceStateInfo;
import com.panda.business.domain.WeChatAccountInfo;
import com.panda.business.domain.req.DeviceStateInfoVo;
import com.panda.business.mapper.CustomerMapper;
import com.panda.business.mapper.DeviceStateMapper;
import com.panda.business.mapper.WeChatAccountMapper;
import com.panda.common.core.domain.AjaxResult;
import com.panda.common.core.domain.entity.SysUser;
import com.panda.framework.domain.base.DBPage;
import com.panda.framework.domain.base.PageBean;
import com.panda.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional // 支持事务
public class DeviceStateService {
  
	@Autowired
	private DeviceStateMapper deviceStateMapper;
	 
	@Autowired
	private CustomerMapper customerMapper;
	
	
	@Autowired
	private ISysUserService accountMapper;
	
	@Autowired
	private WeChatAccountMapper weChatAccountMapper;
	
	 
	public PageBean<DeviceStateInfo> queryDeviceStateInfo(DeviceStateInfoVo info) {
		 
		DBPage page = new DBPage(info.getPageSize(), (info.getPageNo() - 1) * info.getPageSize());
		
		List<DeviceStateInfo> resultList = deviceStateMapper.queryDeviceStateInfo(page,info);
		Integer totalResult = deviceStateMapper.queryDeviceStateInfoCount(page,info);
		int pageTotal = totalResult % info.getPageSize() == 0 ? totalResult / info.getPageSize(): totalResult / info.getPageSize() + 1;

		PageBean<DeviceStateInfo> pageBean = new PageBean<DeviceStateInfo>(info.getPageSize(), info.getPageNo(), pageTotal, resultList);

		return pageBean;
	}
 
	
	public void delete(Integer id) {
		if(id != null ){
			weChatAccountMapper.delete(id);
		}
	}
	
	
	public DeviceStateInfo findDeviceStateInfoByid(Integer id){
		return deviceStateMapper.findDeviceStateInfoByid(id);
	}
	 
	public AjaxResult insert(DeviceStateInfo info) {

		String res = "success";
		Integer count = customerMapper.getCustomerPhoneCount(1);//该客户端的的设备数
		Integer pcount =  deviceStateMapper.getPhoneAllCountByCid(1);//已经绑定的设备数
		if(pcount >= count){
			res = "over";
			AjaxResult.error("信息录入失败！设备数超限");
		}else{
			try {
				info.setCid(1);
				long accountid = info.getAccountid();
				SysUser sysUser = accountMapper.selectUserById(accountid);
				AccountInfo account = new AccountInfo(sysUser);
				info.setDid(account.getDid());
				String deviceid = info.getDeviceid().trim();
				info.setDeviceid(deviceid);
				deviceStateMapper.insert(info);
			} catch (Exception e) {
				e.printStackTrace();
				return AjaxResult.error("信息录入失败！");
			}
		}
		
		return AjaxResult.success();
	}

	public String update(DeviceStateInfo info) {
		String res = "success";
		try {
			long accountid = info.getAccountid();
			SysUser sysUser = accountMapper.selectUserById(accountid);
			AccountInfo account = new AccountInfo(sysUser);
			info.setDid(account.getDid());
			String deviceid = info.getDeviceid().trim();
			info.setDeviceid(deviceid);
			deviceStateMapper.update(info);
		} catch (Exception e) {
			res = "fail";
			e.printStackTrace();
		}
		return res;
	}

 
	
	public boolean binding(long accountid,String ids){
		try {
			if(!StringUtils.isEmpty(accountid) && !StringUtils.isEmpty(ids)){
				SysUser sysUser = accountMapper.selectUserById(accountid);
				AccountInfo account = new AccountInfo(sysUser);
				String []  s= ids.split(",");
				if(null != s && s.length>0){
					for(int i= 0;i<s.length;i++){
						Integer id = Integer.valueOf(s[i]);
						DeviceStateInfo  phone = deviceStateMapper.findDeviceStateInfoByid(id);
						if(null != account && null != phone){
							bangding(phone,id, accountid, account.getDid());
						}
					}
				}else{
					Integer id = Integer.valueOf(ids);
					DeviceStateInfo  phone = deviceStateMapper.findDeviceStateInfoByid(id);
					if(null != account && null != phone){
						bangding(phone,id, accountid, account.getDid());
					}
				}
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	private void bangding(DeviceStateInfo phone,Integer id,Long accountid,Long did){
		if(null != phone && null != did){
			//先绑定 操作员 及部门
			deviceStateMapper.binding(id,accountid,did);
			//再查询设备状态，更新操作员id，保险起见 将该操作员踢下线
			if(null != phone.getDeviceid()){
				WeChatAccountInfo  account = weChatAccountMapper.findWeChatAccountInfoByDeviceid(phone.getDeviceid());
				if(null != account && null != account.getId() && null != accountid){
					weChatAccountMapper.updateAccountidOffline(account.getId(), accountid);
				}
			}
		}
	}
	
  
	
	public void updateLoginTime(String deviceid){
		deviceStateMapper.updateLoginTime(deviceid);
	}
	
}
